include('shared.lua')

local matLight 		= Material( "sprites/light_ignorez" )

function ENT:Initialize()
	self.PixVis = util.GetPixelVisibleHandle()
    
    self.LightAttachment = self:LookupAttachment("light") 

	self.BaseClass.Initialize( self )
end

function ENT:GetLightColor( visible )

	if self:Building() then
		return Color(255, 0,0, 255 * visible)
	end
	
	if IsValid( self.Enemy ) && self.Enemy:IsPlayer() then
		
		self:UpdateAnglesEnt( self.Enemy )		
		return Color( 0, 255, 0, 255 * visible)
		
	end
	
	self:UpdateAngles( 0, math.sin( CurTime() ) * 60 )
	
	return Color( 255, 255, 0, 255 * visible)

end

function ENT:Draw()
	self.BaseClass.Draw( self )
	
	local LightPos = self:GetAttachment( self.LightAttachment ).Pos
	local Visibile	= util.PixelVisible( LightPos, 4, self.PixVis )
	
	if Visibile then
		render.SetMaterial( matLight )
		render.DrawSprite( LightPos , 8, 8, self:GetLightColor( Visibile ) )
	end
end

function ENT:OnRemove()

	self:EmitSound("NPC_FloorTurret.Destruct", 500, 500)
	
	self.BaseClass.OnRemove( self )

end